/*
 * This file is part of the Boring Engine's Test Game.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package testgame;

import com.googlecode.boringengine.Animator;
import com.googlecode.boringengine.DefaultFont;
import com.googlecode.boringengine.Font;
import com.googlecode.boringengine.GameEngine;
import com.googlecode.boringengine.GameLoader;
import com.googlecode.boringengine.Graphics;
import com.googlecode.boringengine.Input;
import com.googlecode.boringengine.Log;
import com.googlecode.boringengine.PauseMenu;
import com.googlecode.boringengine.Render;
import com.googlecode.boringengine.Sprite;
import com.googlecode.boringengine.lua.LuaInterpreter;
import java.awt.event.KeyEvent;

public class Main implements GameEngine {

    private Sprite kirby, screen;
    private Animator awesome;
    private int x = 0, mouseX, mouseY, timered;
    private boolean mouseDown;

    public void init(GameLoader loader) {
        timered = 600;
        OtherClass.init();
        Log.info("Main.VERSION == %s", com.googlecode.boringengine.Main.VERSION);
        kirby = Graphics.loadSpriteFromFile("resources/kirby.sprite");
        awesome = new Animator("resources/kirby", loader);
        awesome.changeAnimation("walk");
        LuaInterpreter.addObject(new OtherClass(), "treetop");
        LuaInterpreter.runScript(null, "resources/sprite.lua", loader);
        PauseMenu.add("Qwerty", new Animator("resources/letters", loader), new Ruinable(), false);
        if (kirby == null) Log.warn("Kirby failed loading");
    }

    public void update() {
        if (x > 800) throw new RuntimeException("Kirby was out of the screen");
        if (timered <= 600 && timered > 0) timered--;
        if (Input.isPressed(KeyEvent.VK_LEFT, 0)) x--;
        if (Input.isPressed(KeyEvent.VK_RIGHT, 0)) x++;
        Input.MouseInfo info = Input.getMouseInfo();
        mouseX = info.x; mouseY = info.y; mouseDown = info.buttonDown;
    }

    public void draw() {
        Render.clearScreen();
        Graphics.drawSprite(kirby, x, x, 2);
        Render.addScale(2);
        awesome.draw(x, 400);
        if (screen != null) Graphics.drawSprite(screen, 400, 50);
        Font f = DefaultFont.getDefaultFont();
        f.draw(0, 550,
                String.format("X IS %d MOUSE AT %d %d%s", x, mouseX, mouseY,
                mouseDown ? " DOWN" : ""));
        f.draw(0, 575, Graphics.getFPSString());
        Render.addScale(.5);
        if (timered == 0) {
            screen = Render.createSpriteFromScreen(0, 550, 150, 50);
            timered = 601;
        }
    }

    public void quit() {}
}
